Some code style cleanup and fix some BUGs