refactor clone function, add full documentation