I'm not happy with this yet, but it seems a reasonable compromise. There is a commented-out method included here too.