Refactored routes, socket handling, and helper functions out of app.js