complete algo, but with bugs