Bug fixes and general improvements