added check to avoid array out of boud access