now it works, a few corner cases left