Refactored many routines into separate functions. Improved stack loading behavior.