Fix minor bugs and refactor codes.