Typo correction and rearranged some logic