Refactor main logic into separate source file.