Refining Castelnuovo-Halphen bounds