amended with corrections from Geoff, Andrey