Refactored and removed out-of-bounds conditions