code reuse. added debugging prints and comments