Refactorized a little bit