Added some array checks to prevent errors