fixing memory leaks and overall performance algorithm tweaks, some layouting