Added some phpunit based tests and a mock class to allow us to isolate components for testing. Rewrote some core elements to better accomodate tests, fixed some small bugs tests revealed.