Improve error checking and file structure