Fixed error checking and declaration of arrays