updated version of the code, from gsocbasic folder of giang