Code refactoring
Added free to release the memory that was captured with malloc
Also, added another method for better code clarity