add tests for write method, consolidate code