Several fixes for some issues.

See the commit descriptions.