Add database configuration logic; minor bugfixes and code cleanup