fix bugs and reorder funcs