Cleaned up a bunch, added documentation.