fixed code to avoid OOM error