Fixes based on answers to my SO question (http://stackoverflow.com/questions/21809092/extremely-slow-r-code-and-hanging).