This would be a good time to modularize this and make it something more easily maintainable and testable (#3).