replace 'and' with '&&' because 'and' does not always work. fix index out of bound issue though the compiler doesn't check it.