Rewrite regex handling, fixed more errors