Added comments, reorganized code.