Refactored to support db connection