Refactor main()
Closes #4