Phpunit

Merging in phpunit test cases, a few bug fixes.