Final commit before merging into master, small refactoring tweaks