Added some optimalization to the code.
Changed to periodic boundaries.