updated error checking so that errors are correctly thrown