refactor the read function

splitting the read function into two smaller functions