Some more bugfixes and simplifications to the code