Refactoring (squashed My and Petro's changes)