Add debug mode, and fix error in merge code