section 3.2, algorithm 3.1 finished and commented
section 3.2 completed: fixes #9, fixes #40