refactoring mostly done and tested