No functional changes, but refactored to better handle open/close db and timing