Fix incorrect implementation of mod