added exception handling and changed txt encoding