Some optimizations and helper class.
Minor refactor.