Fix various minor leaks and refcounting problems in UI