refactored code so that logic is in routes file