Refactor messy boolean logic