Replaced sizeof(str)-1 and magic nums with charsmax