Fix valgrind detected errors

This includes a race condition that would cause crashes at startup.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>