This fixes the issue discussed last Friday. In summary:
This solves part of the bugs found by @bvandenbroucke.