return of determine_replacement()

see the comments in the code before and after this commit for
explanation why. Also did some other clean up stuff.