improved and tested. Compiles and runs, but there are deviations (see @todo).