More code cleanup and regexp optimizations.
Numerous optimizations and code cleanup.