Additional bugfixes and added some tests