simplify function and rewrite rules