Implemented some bash good practices for failing early when there is an error ( http://kvz.io/blog/2013/11/21/bash-best-practices/ )
Fixed a few unbound variables