Algorithm seems to be working correctly.