really ugly. But working. Fixed memory leak

Signed-off-by: Daniel Fagnan <dnfagnan@gmail.com>