safer code with consts
memory leak fixed
using TYPE_NUM
tabs to spaces