Add header file, add tests at main function, refactoring, fix memory leaks.