Added NoSuchElementException and modified error handling