Moved algorithm and clean it up