Database improvements and bug fixes