realised my handling of loops is incorrect