Refactored template contexts and factories. refs #479