Got rid of about 200 lines and made several abstractions.