I have implemented a solution for this (not sure if the optimal one) in 843768c9c0907b127e7f139496580b94e92cac54.