Some optimization & refactoring