Fix some bugs and first tests