(Hopefully) the last update to this pull request

Simplified time-leaking solution, using min() to make sure only valid indexes are called.