fix mem leak and -Wall warnings