Factored code to different files for maintainability