Refactor error message code