Avoiding singularities and numerical instabilities in free energy calculations based on molecular simulations