Refactoring and adding bug fixes.