moved function declarations out of render loops for increased performance