Refactoring and alignment fixes