o Major refectoring after running the code through http://www.shellcheck.net/.