Can this logic be replaced with / and %?