added better error checking