Shuffling around code and adding comments

Code moved around, and sometimes into more appropriate files