slight optimization to problem 146 plus code cleanup