Refactored to make more sense as a library