Corrected code as recommended by nickdesaulniers