refactored code and won 2 sec on runtime on 50 repeats