Refactored into useable code for JB