better error checking, minor issue correcting