buffer now accounts for invalid chars