Refactoring. Fixed merging bugs.