Reorganize to share more code