cleanup code a bit, add statistics