major refactoring. not complete yet