refactored error handling - fixed 500 errors where 400 should served