Added most of the cost division logic