Changed to use discriminator (reverse-merged from commit d9279a1a09796ce03f3cc9ef45ee184a75938c02)