massive refactor!