use more ugly but hopefully more robust method