Bug fixes and code style