moved function into conditional