Midway through cleaning up this method.
Cleaned up code, added a bunch of comments, and renamed some variables for readability.