improved write and added setOutput
cleaned mistakes and added API