Extract write_main, write_private, write_public methods