Complete implementation that pasts all the tests