Think I finally got it, needs testing