fixed a typo, rearranged some functions