Misc fixes on code style and logic