Latest version with patterns and cleaner codes.