refactoring controller code