Added #as_json methods. Cleaned up the code.