added msg_length checking