most optimized solution so far