Extracts logic from routes to helper functions