Fixed an error when reading in files; Code tidying