Fix code to only call function if we need to