Test improvements. Bug fixes