refactor complex condition, thanks for @mayah