Getting screwy errors. Implemented the algo but not working fully