Add validation tests, fix some bugs