Fix linting issues and DRY code

I ran the linter and fixed those issues, as for the DRY code, while some
parts of the code are similar, they return different values on different
user events. Thus not letting us condense the code into one function,
since the two functions use specific variations of the code.