The main problem was you were not actually returning the new value and you weren't updating the input variable to the "new" value anyway, which meant the initial illegal value was used for the for...