not working yet, but quite a bit of the logic implemented