refactored a little bit