added PostgreSQL compatibility and formatted code heavily