Broke up functions

Cleaned a lot, made readable, added comments.