Refactored app factory and respecting the application context more