added tests and fixed
fixed memory error