Code clean up and rewriting.