changed stack pushing/popping into a RAII wrapper object to make it exception safe and more modular