Fixed threading issues and better database handling closes #54