fix changed checking order to avoid possible access violation