Fixed a few bugs.
Added lots of testing. Only a bit more to go.