refactored a lot still very messy