[WIP] fixes

I've made some changes. You may cherry-pick what you like or discuss them here.