This should do it. Needs testing.