Refactor 501 handling