Previous version just before removing a lot of code that is not consistent with the version described in the paper.
Some changes in order to include the cases discussed in ftp://cmp.felk.cvut.cz/pub/cmp/articles/matousek/Sandr-TR-2009-04.pdf