Refactoring and file manip in image utils