Fixed the implementation. More elegant. And passing all tests.