passing the tests (still unfinished)
first version that seems to work (cleanup needed)