bug fix, now works to write
organized and fix up