added optimized solution to problem