added end condition for reading