fixes to original code by Thomas Luft