Moved do-while loop to own function