refactor gem, lib logic