Added an ApplicationFactory and did some more code refactoring.