Add unit tests, cleanup app code so its more testable