Improved object and file IO to be more robust

Also added unit tests for the changes