restructured to use internal rewrite rules