Minor changes to code efficiency, error checks, and cleanup.  Fully working and finished version