added json encoding/marshaling, cleaned up some code and made it messier in the process