Various bug fixes and closing open file handles.