Database now closes connection after each query and reconnects if closed to ensure no more connection and locked database problems.

Fixed more internal 500 errors.