Fixed to be more memory-efficient