Added conditional statements to make it bullet proof