Refactoring and fixed merging bugs