Implements #822 (partially)