Fix stub paths and refactor write

Please review carefully, as I probably could not see and test the whole impact of my changes. Also fixes  regression in https://github.com/yury/ib/pull/58