@starsirius Refactor with class patterns

cc @beamjet.