Depending on which PR gets merged first, #1643 might simplify this (by one line ;)