Script finally working as a cron job now. Some fixes.