Rewrote length()

This should work much better, and it's easier to read