Fixes, fixes, fixes

Fixes for a bunch of bugs and some refactorizations