added slightly better error checking