fix reading unmalloced memory (aka don't trust the internet)
fix valgrind errors