modified from hulob sql