updated algorithm + comments