Added tests and refactored application a bit to optimize for testability