Issues #665, #666, #667

Pull request by Anton.