massive refactoring. code is much cleaner