Fix a few bugs and add first tests