Bug fixes, refactoring, and performance issue fixes