all regexp in a function

also some minor changes to code structure