Refactor of some helper functions