slightly too(?) complex list pattern matching