Fix replace_node_at

The former api function replace_node_at() did not work correctly. 
Update this function with the same logic as get_node().