fixed some bugs and improved the quality of the code to build/extract data

Benjamin Mako Hill || Want to submit a patch?