Refactored codes for optimizing potentials for loop sampling