refactor + Fix some bugs