refactored, first version working