spend function refactor with data.table