refactor code to use yield block