various bug and leak fixes