fix bugs, add test and merging

Ok, I'll try to imagine something.