Refactor code into the main() function