rewrite the code hand by hand