added malloc_s (safe) function, many code improvments/fixes