refactored annagram to put everything inside one while loop