added better conditional checking