Extracted code from PR129 as I didn't want the Spot related changes