fix bugs and make code easy to read.