Cleaned up to reduce code duplication.