This is @kusumotonorio's patch but his branch is not up to date so there are a ton of merge conflicts so I'm just pulling his changes into a new patch. Sorry for my lack of git to do this cleanly while maintaining his credit for this patch.