cleaned up script to fix errors and more efficent.