added SAM writing