moved stuff around, fixed memory leak