Better logic and less code for previous commit