fix buffer overflow and add logging