Merge code and fix bug