Copy recursive function, code cleanup