General cleanup

Moved the functions around, added comments.