This is a ton of copy-paste code. Is it possible/desireable to merge the real and complex versions of this into a single template?