tidied up code for rendering template