udpated version which still needs a lot of cleanup, but works with the json API