Fix some bugs. Optimize for memory using
Merge conflict