Various bug fixes, see individual commits