fixed implementation of previous optimization