Various fixes and memory usage optimization;