Fix #501 with the code from @Borkason