improved error checking