debugged so fxn is more flexible