Short version of script works as expected