Refactor code to reduce main method length