Much more efficient version of previous commit.

Probably a little less robust, but should be ok.