Issue 626 by Pjohans: make sure to clean up if something goes wrong on loading entities