added better error check