fixed bug

Needs improvement with memory usage