added solution 7 and subsequent refactoring