Fixed problems and the script now produces the correct answer