Huge refactoring done on most of the code.

Also, doing away with this whole thing, and implementing it fresh.