factor out image selection logic