Rewritten pattern matching without K

This is a rephrasing of #674.