[WIP] debug memory issues as reported by valgrind