Modified to work with PostgreSQL