move to prototype and fix loops