fixes some more bugs, add some tests