refactor code and move routes to the top