Refactored code to centralised db access

Also added mixpanel stats