first version of main algorithm ready, needs testing