Needs a little more error checking but works