added headers, abstracted writing