Major refactoring, add test files