Note: this is highly experimental and will likely change in the future.
Alternative implemention of #1727