Imported code from ianmstew/Billing.git#d9a3f0977a08 and enhanced for standalone project