refactor while loop and rename methods to more descriptive