Reorganized some code and updated doc blocks.