Partial fixes for #24

Somewhere in my other branch, everything stops working. I'm cherry-picking, testing and applying commits one-by-one.