debugged periodic boundaries