fixed a lot of important gameplay bugs, refactor codes