Refactored code. Templates are now fetched during state transition.