implementing length, refactored code