handle OOM problem
fix a logic error