copied from https://github.com/rupa/z/tree/7f2b9189a0f45d693f26a299b5fc8a82ba81493d