fixed array access problems. moved some checks around