added optimized version as comment