Fix invalid memory access and wrong # parsing