removed all maps, forEach and reduce replaced with while