Refactor ewald() and ewald2() subroutines