Added some checks for buffer overflows