achieved solution with better runtime