Added changes suggested by Caleb, including removal of some redundant code to make it faster, and use things discovered by other plugins