Corrected some spelling and refactored some logic in a few places