re factor code