fixed wrong math in calculating indices, memory leaks and pointer aritmetics, everything works now