Cleaned up, replaced foreach with json_encode.