Small refactoring, add assertions.
Improve error handling if reading file.