Refactor error checks