added (untested) code for extending to >2 images