refactored some while statements to if statements for readability