this seems to work reasonably