Fix some bugs + tests.