Wrapping script in functions

Moving primary functionality inside of a main script and adding some
TODOs.