improved the script. Still missing some cases.