moved some logic to helper functions