first version that seems to work, added some tests