Refactor and add new rewrite rules