Is there any better way to do this? I got this to work, but it seems like a bit of a hack and requires some interesting monkey patching.