Add Tests

Add tests and various other fixes.