Refactor ugly method