changed tests to deal with errors (versus panic)
removed most panics - added encode to json and write method.