refactored a lot