Refactored; print extra diagnostics.