Squashed commit of some code refactoring