added write mode. in the course of refactoring stuff.