fixed bugs, refactor complete