Optimize previous commit

There are simpler ways to solve this problem. Here is one.