Move self reference check to a better location