almost finished refactoring