Template code refactor