Refactor data_template function