Bug 495 v2

This a reworked patch for fixing issue 495