Refactor to library & other enhancements