add tests and fix first round of bugs