Added reference handling code

current issue : doesnt insert refrd nodes in the right place