Add two if statements to fix bug