neaten some of the pattern matching code