Fixes issue #370 and parts of #371.