moved a lot of code around but it kinda works