Solving for reference number and terminal point