fix bugs where it does not work properly as cron job