Refactoring and unit tests for 500 and 404 responses