added lots of bits for templating, moved around chunks