fix broken logic from code refactor and simplify code