Final refactoring of code before merge.