Applied fix suggested in bug 3282 and shortened code
Merge pull request #1495 from StevenPuttemans:bugfix_3282