Testing and refactoring, general cleanup