adding early exist
changing replacing String arrays by ArrayLists for convenience