this script is fubar. i will revisit later.