Cleanup: Some Memory leak fixes and program-flow cleanup