Cleaned up error checking