Simpler and more efficient algorithm
1.2.1