Bug fix and code style changes