Re-wrote 3way mod function..