Small changes + attempt to remove more redundancy

I've tried to do the same thing with the edges that I did with the
sides: to use a for-loop instead of three ifs, but it seems that it
won't work properly,and  I don't know why. I will look into it later.