updated cache to use min instead of sorted, improved stdin read