Re-structure script - using functions

Put some logic :)