refactored insertNode method