modified patch from Hans Z