Slightly better error handling for unreadable files. Minor refactoring