Script rewritten, functionality added.