based on an original diff by Mikolaj Kucharski, with many tweaks by me feedback and ok steven@ and alek@
Work done primarily by Francois Briere, thanks Francois.
From Thomas Delaet.