Improve logic of previous commit