Fix some bugs and change/add tests