ENH: refactor code