Add counter, tidy up code