Fixed mem leaks, added tests and ternary operator