fixed chunk size calculations and replaced ftell