moved from plyr to data.table for speed