Several fixes.
merged with upstream.