Fix some bugs, simplify the code and some layout changes for code guidel...

...ines