first implementation of FS Algorithm