Refactor logic into function and renaming.