fixed some bugs and merged codes