Impreved IO error handling and created tests