Added much simpler algorithm