Fix memleaks and bugs

Signed-off-by: Markus Pargmann <mpargmann@allfex.org>