Remove trailing whitespace
Slightly refactor and fix tests