Moved image loading code to a separate function.