Added sampling function and tests