Based on patches by Victor Lin <bornstub@gmail.com> Fixes bug #550230.
Based on a patch by Chris Robinson <chris.kcat@gmail.com> Fixes bug #615615.