Add doc and clean up the code.