refactor, fix bugs (stable)