Added routing logic for 404, refactored a bit to prevent repeating code.