done a bit refactoring for better redability
tested successfully