yareg's code refactored for the sake of (no fake) easy reading