generalized the extract and replace method

removed lots of helper functions and made it easier to collect and
replace differnt types of nodes. Removed tests for those functions.
Added tests for the new functions.