revised patch for issue 246