Refactoring and tweaks