fix lot's of bugs and add tests