cleaner versrion of stacking code