Add application tests. And more refactoring.