Quaternions

I’m using quaternions for 3d look but i found strange thing - when rotating around x axis and turned to negative z axis i move look up and down as i want but when I turn to positive z axis and then rotatine around x axis I get reversed look(when it should turn up it turns down) and if I turn 90 degrees on y axis then moving up or down it twists view(probably rotation on z axis) Where can the bug be ?