General refactor and cleanup.