debugged simex.sgp function