Fix total_len calculation