Refactoring of endless loops and some type accuracy.