Fixed memory leak and removed heap allocation