fix buffer overflows and format issues