Fixed bugs with writing out results