Moved logic to lib