First version of bugfix for #473.

and some other stuff.