Refactored main lib