fix out-of-bounds mem access (for valgrind)
fixed stack balance bug, code cleanup
fix editing error