tidied up pattern matching