Sorted occurrences and refactored logic into methods.