Hacked together the basic logic