Refactored database connections

Moved database connections to separate file and now handle reconnections