fix the lower division bug and refactor the code