implemented more efficient solution