Fix for issue 1333 with help of @tiagochst