I think I got the collision detection working properly, but the code is a bit messy