Made before hook more flexible
Added test 'stack' for better debugging