src: Some cleanup and fix memleaks