applied several fixes from Andreas Schmid