Improve image resizing function