Some changes: Add save method, and reorganize code.