Fix bug in error checking