refactoring this mess