refactored, and added a write method.