Fixed postgres bug and re-wrote database session handling.