moved template stuff into separate functions