Added calls of free function, performed refactoring