Propery encode nested objects.
Fix some bugs.