refactoring code in controller