Added the missing ends to the if statements