refactor a bit and removed memory leaksrefactor a bit and removed memory
leaks..