removed some more test code
a solution that works a little better, though a demo solution online gives different result.