Fixed bugs and refactor classes