Fix some bugs + new tests