merge code and fix some bug