updated to work properly with postgresql