Bug fixes and code style improvements