Fix stub paths and refactor write

Please review carefully, as I probably could not see and test the whole impact of my changes