Fix spelling errors and some refactoring