major refactoring, better tests