CLN: For loops, boolean conditions, misc.
- Use generator comprehensions where the resulting list comprehension was not used further
- Avoid multiple unnecessary loops
- Simplify boolean assignments
- Test error conditions first before proceeding
- Removed private, unused function

