code rewrite for rethinkdb