improve if loop flow