Bugfixes and feature enhancement.

Now it replaces rules with correct tokens.