code refactored, support left and right buttton