All tests passed. Final. More revision needed for the better algorithm