Functional Homework!
Improved Algorithm