working version

I think the main problem was that you were using alignment length instead of itag Length.  I cleaned up some other stuff as well.

Writing the results to a text file is not working.  I leave it to you to fix that.  The screen output works.