Fixed lot of coding errors and bugs.
Scripts tested on 4 port card.
TODO: send_email is pending.