modifying to suit my needs - still has debug in it!