You may find this refactoring useful
https://gist.github.com/lunatyq/ebd5ed6feb1af5205c7