Refactored somewhat.