improved some functions and fixed some bugs.