Refactoring the code. Added proper controller.