added better error checking in debug