Memory leak fix. Moving flatten() from main program to Image::flatten()