Finished the first abstraction layer.

Also did some cleaning up.