Fix some memory leaks and improve allocations (#26)

Signed-off-by: Jorge Ruesga <jorge@ruesga.com>