refactored much of stuff