Use modulo instead of manually looping across boundaries