Adjusted periodic boundary and fixed a few bugs.
Perfectly working version.