fix bugs and merge code