refactor using array instead of linked list