Bug fixes on db connection code