Overall refactoring (partially due to hlint).