Fix memory leaks and alignment issues (thanks rixed at happyleptic.org)