Quick code cleanup. Rewrote a few of the more hacky implementations.