Memory leak fix, code organization, db update performance enhancements