Refactored the code to use node ptr