Rearranged the code a little so it made sense, and adjusted some comments