Refactor code (#628)

* Remove duplication