fork by webistrano,and modify some function.