finished the logic but not comitting as it needs some improvement