Rewrite patterns module. Fix parse bug