largely refactoring