cleanup code and add JSON hooks