refactored error checking into its own method