Stable version - Refactored DB logic