a lot updates
1. for loops change to while loops
2. new helper functions
3. new algorithm
but the performance remains the same