based on an original diff by Mikolaj Kucharski, with many tweaks by me feedback and ok steven@ and alek@
From Thomas Delaet.