refactored, fixed rewrite rules