Adding code from Rails spike repository.