finished function for replacement now time for testing and debugging.