refactored reading/writing from/to file to methods