Tests for main app and some refactoring