Major revision as per @pwaters