Fix memory leaks and incorrect use of widgets