Fix bug in provisioning
Add tests and fix bugs they uncovered