Fix some bugs, simplify the code and some layout changes for code guidelines