More cleanup, moving variable initializations to the top, documenting, etc.