Fix implementation of mod