refactored code and created controller