Masonite swings to profit in Q2