added performance stats and cleaned up code