merge codes and fix bugs