Added handling for invalid word lengths