fixed some bugs and separate some codes