Added a bunch of functions and comments, more WP formatting. Fixed some bugs that crept in after the refactor.