Fixed memory leak and cleaned up formatting