completed code rewrite.