Finished up code committed earlier. This should work now but is untested.