Added length checks
Refactored an expression