Fixed logic ordering to prevent AttributeError