Replaced stack function with modularized code