Fix some bugs. Refactor some classes.