James C - pulled out type
James C - more refactoring
James C - scope now a function