improve code but having some bugs that are big and
incomprehensible