Revert "Revert "Lots of changes (see extended description)""

This reverts commit 96afa2ed6dbddd8e4ba4a847dcf0b123714259b3.