added some debugging code and some perf. improvements