add tests and fix discovered bugs