Added many of the fixes in pull request #21 from OptimusCrime/more-structure