Cleaned up some code with the context managery use of 'open'.