code from blog entries (hansewetz@blogspot.com)