Fixed crash when closing image in some cases.
Also fixed wrong image index when loading from CLI or file manager