Fixing reading when buffer is larger than available data