These can do with some optimization. A majority of this work is based off an earlier patch submitted by Shreyas Prasad <sprasad@appne....>.