This script is not perfect and probably has more flaws than what you mentioned. It was a quick hack to solve a problem and works fairly well and is fairly accurate but by no accounts is it perfect.
Thanks for the input, will try to address your points.