Fixed out of sync and duplication error

Fixed the scenarios where the chunking can cause an out of sync insert with the left over and also duplication creation.
Merge pull request #5 from slakbal/patch-2

Fixed out of sync and duplication error