small optimizations, improved regexp handling