better logic fixes edge cases, also removes for-loop = ~3x faster
better logic fixes edge cases, also removes for-loop = ~3x faster

vlulla/data.table,Rdatatable/data.table,jangorecki/data.table,vlulla/data.table,jangorecki/data.table,Rdatatable/data.table,vlulla/data.table,jangorecki/data.table,Rdatatable/data.table,Rdatatable/data.table,jangorecki/data.table,vlulla/data.table