fix bugs, final version