refactoring, style fixed, bug fixes