Fix some bugs with and optimize the initial loading of a file