Some change to add flexibility and remove memory leaks