Add tests, fix some bugs. Function name/style refactoring