moved code to a more modular structure and made it robust with regards
to the set -e option