Code cleanup and added statistics