Some bugs-solving and refactoring