Refactored code to save/load images