Refactor next/prev image functions.