Implement QuickCheck tests, and fix some issues