incorporating some small tweaks from a local version