Implements #1404, needs testing