rewrite f() to be much faster