added simple version of algorithm 2
refactored