Refactoreed the main controller