Changed modular arithmetic a little because C is the devil