Completion of PR 486

I took @dlouwers' #486, rewrote his commit messages, merged explicitly with master (was the cleanest and most explicit way of catching up), and added some improvements.