Fixed so it checks all boundaries properly and averages accordingly.