Refactoring

Various changes for code cleaning and small tweaks