refactored anagrams code