introduced caching and fixed a bug. Also added some more output.