huge changeset, modularized the program and rewrote parts to use more recommended pythonic patterns