refactor code with upto method