def check_index(index, valid_min, valid_max):
    """Check that an index doesn't exceed the valid bounds."""
    
    if index >= valid_max:
        index = index - valid_max
        
    if index < valid_min:
        index = index + valid_max
 
    return index