Refactor with templates