refactored code

scripts have been refactored to be more modular - so that the secure and
normal scripts can be implemented in an easier and more readable fashion