Fixed some typos, rearranged some code slightly.