patCount reimplemented with a more concise functional solution