buy function almost done