partial code rewrite