Use basename more
Group matches into match groups
Move commonly-accessed options into globals
Remove useless variables