code a little bit refactored