Fix some bugs and merge latest code