Optional, but I'd try to refactor just the image generating part of this into it's own method, or maybe even a utility class in /lib