move code into if test