refactored numerous parts