Fix some bugs + tests