Move counter into function