Optimize memory, remove unused code